import { app, BrowserWindow, Notification, globalShortcut, ipcMain } from 'electron';

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow = null;

const createWindow = (parentID) => {

  let parentWnd = null;
  if (parentID > 0) {
    parentWnd = BrowserWindow.fromId(parentID);
  }

  // Create the browser window.// 背景透明很多问题，会造成最大化最小化消息收不到
  let winhandle = new BrowserWindow({
    width: 1000,
    height: 800,
    frame: false,
    //transparent: true,// 只有不开启了调试工具才有效
    show: true,
    //backgroundColor: '#2e2c29',
    parent: parentWnd != null ? parentWnd : null,
    modal: parentWnd != null ? true : false

  });

  if (mainWindow === null) {
    mainWindow = winhandle;
  }
  console.log('current win Count:%d', BrowserWindow.getAllWindows().length);
  /*
  winhandle.once('ready-to-show', () => {
    console.log('main ready-to-show');
    ` `.show();
  })*/
  

  // and load the index.html of the app.
  //if (WinArray.length == 1) 
  if (1) {
    winhandle.loadURL(`file://${__dirname}/index.html`);
  } else {
    winhandle.loadURL(`http://www.baidu.com`);
  }

  // Open the DevTools.
  //winhandle.webContents.openDevTools();

  // Emitted when the window is closed.
  winhandle.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    let winCount = BrowserWindow.getAllWindows().length;
    console.log("receive close, winCount:%d", winCount);
    winhandle = null;
  });
/*
  winhandle.on('maximize', () => {
    console.log('main maximize');
  });

  winhandle.on('minimize', () => {
    console.log('main minimize');
  });
 */

  // 全局注册快捷键
  globalShortcut.register('CommandOrControl+Y', () => {
    // Do stuff when Y and either Command/Control is pressed.
    winhandle.webContents.send('action', 'btnNotify');
  })
  winhandle = null;
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => { createWindow(-1) });

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow(-1);
  }
});


//监听与渲染进程的通信
ipcMain.on('action', (event, arg) => {
  switch (arg) {
    case 'exit':
      //做点其它操作：比如记录窗口大小、位置等，下次启动时自动使用这些设置；不过因为这里（主进程）无法访问localStorage，这些数据需要使用其它的方式来保存和加载，这里就不作演示了。这里推荐一个相关的工具类库，可以使用它在主进程中保存加载配置数据：https://github.com/sindresorhus/electron-store
      //app.quit();//退出程序
      break;
    case 'kNewWin':
      console.log('receive kNewWin:ID:%d', event.sender.id);
      if (1) {
        console.log('new kNewWin');
        createWindow(event.sender.id);
      }

      break;
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
